<?
if( !defined( 'MODULE_FILE' ) ) {
	die( "cant access file directly!" );
}

autoLoader();

$mid 	= ( ( true == isset( $_GET['mid'] ) && true == valStr( $_GET['mid'] ) ) ? $_GET['mid'] : NULL );
$smid 	= ( ( true == isset( $_GET['smid'] ) && true == valStr( $_GET['smid'] ) ) ? $_GET['smid'] : NULL );
$emp_id = ( ( true == isset( $_SESSION['SESS_MEMBER_ID'] ) && true == valStr( $_SESSION['SESS_MEMBER_ID'] ) ) ? $_SESSION['SESS_MEMBER_ID'] : NULL );

$objDepartment 		= new BaseDepartments();
$objDepartments 	= new Departments();
$arrobjEmployees 		= new Employees();

$arrobjDepartments 	= $objDepartments->fetchAllDepartments();
$arrobjEmployees 		= $arrobjEmployees->fetchAllEmployees();

$arrobjDepartmentTypes = new DepartmentTypes();
$arrobjDepartmentTypes = $arrobjDepartmentTypes->fetchAllDepartmentTypes();

?>
<html>
<head>
<?php require_once 'commonJS.php'; ?>
</head>
<body>
	<div style="margin-left:20px; margin-top: 10px;">
		<p>
			<a class="btn btn-outline btn-primary btn-xs" id="active" href="modules.php?file=department&mid=<? echo $mid;?>&smid=<? echo $smid;?>" >Create New Department</a>&nbsp;
			<a class="btn btn-outline btn-primary btn-xs" id="active" href="modules.php?file=view_department&type=view&mid=<? echo $mid;?>&smid=<? echo $smid;?>" >View All Departments</a>
		</p>
	</div>
<div class="col-lg-8">
<div class="panel panel-primary">
<div class="panel-heading"><i class="fa fa-bell fa-fw"></i>Department</div>
<div class="panel-body">
	<form METHOD="POST" action="modules.php?file=insert_department&type=insert&mid=<? echo $mid; ?>&smid=<? echo $smid; ?>" id="formSubmit" name="formSubmit" >
		<table width="80%" cellspacing="0" cellpadding="3">
		<?php
				$intDepartmentId 		= NULL;
				$strName 				= NULL;
				$strDescription 		= NULL;
				$intDepartmentTypeId 	= NULL;
				$intParantDepartmentId 	= NULL;
				$strDepartmentHeadName 	= NULL;
				$intDepartmentHeadId 	= NULL;
				$strOfficeNumber 		= NULL;
				$intEmployeeCount 		= NULL;
				$strAddress 			= NULL;
				$strContactPerson 		= NULL;
				
				if( true == isset( $_GET['department_id'] ) && true == valStr( $_GET['department_id'] ) ) {
					$intDepartmentId = $_GET['department_id'];
					
					$objDepartment = $objDepartments->fetchDepartmentsById( $_GET['department_id'] );
					
					if( true == valObj( $objDepartment, 'BaseDepartments' ) ) {
						$strName 				= $objDepartment->getName();
						$strDescription 		= $objDepartment->getDescription();
						$intDepartmentTypeId 	= $objDepartment->getDepartmentTypeId();
						$intParantDepartmentId 	= $objDepartment->getParantDepartmentId();
						$strDepartmentHeadName 	= $objDepartment->getDepartmentHeadName();
						$intDepartmentHeadId 	= $objDepartment->getDepartmentHeadId();
						$strOfficeNumber 		= $objDepartment->getOfficeNumber();
						$intEmployeeCount 		= $objDepartment->getEmployeeCount();
						$strAddress 		    = $objDepartment->getAddress();
						$strContactPerson 		= $objDepartment->getContactPerson();
					}
				} else if( true == valObj( $objDepartment, 'BaseDepartments' ) ) {
					$intDepartmentId = $objDepartment->fetchNextId();
				}
				?>	
			<tr><td height="10" colspan="2"><input id="department_id" Name="department_id" type="hidden"  style="width:100px;"  value="<?php echo $intDepartmentId;?>"></td></tr>
			<tr>
				<td>Department Type:</td>
				<td height="10">
					<select name="department_type_id" id="department_type_id">
						<option id="department_type_id_null" value="">--Select Department Type--</option>
					<?php
						if( true == valArr( $arrobjDepartmentTypes ) ) {
							foreach( $arrobjDepartmentTypes as $objDepartmentType ) {
								$strSelected = ( $intDepartmentTypeId == $objDepartmentType->getId() ) ? 'selected="selected"' : NULL;
								echo '<option ' . $strSelected . ' id="' . $objDepartmentType->getId() . '" value="' . $objDepartmentType->getId() . '">' . $objDepartmentType->getName() . '</option>';
							}
						}
					?>
					</select>
				</td>
			</tr>
			<tr valign="top">
				<td class="mandatory" width="220">Name</td>
				<td><input id="name" Name="name" type="text" class="validate[required]" style="width:202px;" value="<?php echo $strName; ?>"></td>
			</tr>
			<tr><td height="10" colspan="2"></td></tr>
			<tr valign="top">
				<td class="desc" width="220">Description</td>
				<td><textarea name="description" rows="5" cols="40"><?php echo $strDescription; ?></textarea></td>
			</tr>
			<tr><td height="10" colspan="2"></td></tr>
			
			<tr valign="top">
				<td class="mandatory" width="220">Parent Department</td>
				<td>
					<select name="parent_department_id" id="parent_department_id">
						<option id="parent_department_id_null" value="">--Select Parent Department--</option>
					<?php
						if( true == valArr( $arrobjDepartments ) ) {
							foreach( $arrobjDepartments as $objParentDepartment ) {
								$strSelected = ( $intParantDepartmentId == $objParentDepartment->getId() ) ? 'selected="selected"' : NULL;
								echo '<option ' . $strSelected . ' id="' . $objParentDepartment->getId() . '" value="' . $objParentDepartment->getId() . '">' . $objParentDepartment->getName() . '</option>';
							}
						}
					?>
					</select>
				</td>
			</tr>
			<tr>
				<td colspan="2" height="10px"></td>
			</tr>
			<tr valign="top">
				<td class="mandatory" width="220">Department Head Name</td>
				<td>
					<select name="department_head_id" id="department_head_id">
						<option id="department_head_id_null" value="">--Select Department Head--</option>
					<?php
						if( true == valArr( $arrobjEmployees ) ) {
							foreach( $arrobjEmployees as $objEmploye ) {
								$strSelected = ( $intDepartmentHeadId == $objEmploye->getId() ) ? 'selected="selected"' : NULL;
								echo '<option ' . $strSelected . ' id="' . $objEmploye->getId() . '" value="' . $objEmploye->getId() . '">' . $objEmploye->getName() . '</option>';
							}
						}
					?>
					</select>
			</tr>
			<tr>
				<td colspan="2" height="10px"></td>
			</tr>
			<tr valign="top">
				<td class="mandatory" width="220">Office Number</td>
				<td><input id="office_number" Name="office_number" type="text" class="validate[required]" style="width:202px;" value="<?php echo $strOfficeNumber; ?>"></td>
			</tr>
			<tr>
				<td colspan="2" height="10px"></td>
			</tr>
			<tr valign="top">
				<td class="mandatory" width="220">Emplyee Count</td>
				<td><input id="employee_count" Name="employee_count" type="text" class="validate[required]" style="width:202px;" value="<?php echo $intEmployeeCount; ?>"></td>
			</tr>
			<tr>
				<td colspan="2" height="10px"></td>
			</tr>
			<tr valign="top">
				<td class="desc" width="220">Address</td>
				<td><textarea name="address" rows="5" cols="40"><?php echo $strAddress; ?></textarea></td>
			</tr>
			<tr>
				<td colspan="2" height="10px"></td>
			</tr>
			<tr valign="top">
				<td class="mandatory" width="220">Contact Person</td>
				<td><input id="contact_person" Name="contact_person" type="text" class="validate[required]" style="width:202px;" value="<?php echo $strContactPerson; ?>"></td>
			</tr>
			<tr>
				<td colspan="2" height="10px"></td>
			</tr>
			<tr valign="top">
				<td class="mandatory" width="220">&nbsp;</td>
				<td>
					<?php
						if( true == isset( $_GET['department_id'] ) && true == valStr( $_GET['department_id'] ) ) {
							echo "<input type=hidden name=operation value=update>";
							echo "<input type=submit value=UPDATE id=Submit name=Submit>";
						} else {
							echo "<input type=hidden name=operation value=insert>";
							echo "<input type=submit value=SUBMIT id=Submit name=Submit>";
						}
					?>
				</td> 				
			</tr>
		</table>
</div>
</div>
</div>	
</form>
</body>
</html>